package cn.com.io.day20151214.control;

import cn.com.io.day20151214.pojo.Person;

import java.io.*;

/**
 * 对象引用序列化(二)
 */
public class SerializeMutable {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("mutable.txt"));//创建一个ObjectOutputStream输出流
            Person person1 = new Person("张三",34);
            oos.writeObject(person1);//系统将把person1对象转换成字节序列并输出
            person1.setName("李四");//改变person1对象的name属性
            oos.writeObject(person1);//系统只是改变输出序列化编号，所以改变后的name不会被序列化

            ois = new ObjectInputStream(new FileInputStream("mutable.txt"));//创建一个ObjectInputStream输入流
            Person personGet1 = (Person) ois.readObject();
            Person personGet2 = (Person) ois.readObject();
            System.out.println(personGet1 == personGet2);
            System.out.println(personGet2.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(oos != null ){
                oos.close();
            }
            if (ois !=null){
                ois.close();
            }
        }

    }
}
